<!DOCTYPE html>
<html>
    <head>
        <!-- 页面meta -->
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>健康管理</title>
        <meta name="description" content="健康管理">
        <meta name="keywords" content="健康管理">
        <meta content="width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no" name="viewport">
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
        <!-- 引入样式 -->
        <link rel="stylesheet" href="../plugins/elementui/index.css">
        <link rel="stylesheet" href="../plugins/font-awesome/css/font-awesome.min.css">
        <link rel="stylesheet" href="../css/style.css">
        <link rel="stylesheet" href="https://cache.amap.com/lbs/static/main.css"/>
        <style>
            #tip {
                background-color: #ddf;
                color: #333;
                border: 1px solid silver;
                box-shadow: 3px 4px 3px 0px silver;
                position: absolute;
                top: 10px;
                right: 10px;
                border-radius: 5px;
                overflow: hidden;
                line-height: 20px;
            }
            #tip input[type="text"] {
                height: 25px;
                border: 0;
                padding-left: 5px;
                width: 280px;
                border-radius: 3px;
                outline: none;
            }

            .abow_dialog {
                display: flex;
                justify-content: center;
                align-items: Center;
                overflow: hidden;
            }
            .el-dialog {
                margin: 150px auto !important;
                height: 65%;
                overflow: hidden;
            }
            .el-dialog__body {
                position: absolute;
                left: 0;
                top: 54px;
                bottom: 0;
                right: 0;
                padding: 0;
                z-index: 1;
                overflow: hidden;
                overflow-y: auto;
            }
            .amap-sug-result { z-index: 9999; }
        </style>
    </head>
    <body class="hold-transition">
        <div id="app">
            <div class="content-header">
                <h1>首页<small>公司分布图</small></h1>
                <el-breadcrumb separator-class="el-icon-arrow-right" class="breadcrumb">
                    <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                    <el-breadcrumb-item>公司分布图</el-breadcrumb-item>
                </el-breadcrumb>
            </div>
            <div class="app-container">
                <el-tabs v-model="activeName" @tab-click="handleClick"  style="margin: 5px">
                    <el-tab-pane label="分布图" name="first">
                        <div id="container" style="width:98%; height: 600px;margin: 5px auto;"></div>
                    </el-tab-pane>
                    <el-tab-pane label="详细信息" name="second">
                        <div class="box">
                            <div class="filter-container">
                                <el-input placeholder="公司地址" v-model="pagination.queryString" style="width: 200px;" class="filter-item"></el-input>
                                <el-button @click="pagination.currentPage=1;findPage()" class="dalfBut">查询</el-button>
                                <el-button type="primary" class="butT" @click="handleCreate()">新建</el-button>
                            </div>
                            <el-table size="small" current-row-key="id" :data="dataList" stripe highlight-current-row>
                                <el-table-column type="index" align="center" label="序号"></el-table-column>
                                <el-table-column prop="address" label="公司地址" align="center"></el-table-column>
                                <el-table-column prop="longitude" label="所在经度" align="center"></el-table-column>
                                <el-table-column prop="dimension" label="所在维度" align="center"></el-table-column>
                                <el-table-column label="操作" align="center">
                                    <template slot-scope="scope">
                                        <el-button type="primary" size="mini" @click="handleUpdate(scope.row)">编辑</el-button>
                                        <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
                                    </template>
                                </el-table-column>
                            </el-table>
                            <div class="pagination-container">
                                <el-pagination
                                        class="pagiantion"
                                        @current-change="handleCurrentChange"
                                        :current-page="pagination.currentPage"
                                        :page-size="pagination.pageSize"
                                        layout="total, prev, pager, next, jumper"
                                        :total="pagination.total">
                                </el-pagination>
                            </div>
                            <!-- 新增标签弹层 -->
                            <div class="add-form">
                                <el-dialog title="新增检查项" :visible.sync="dialogFormVisible" :append-to-body="true" :modal-append-to-body="false">
                                    <el-form :model="formData" @submit.native.prevent>
                                        <el-form-item style="margin-left: 30px">
                                            <el-input v-model="formData.address" id="addInput" autocomplete="off" style="width: 70%" @click='this.formData.name=""'></el-input>
                                            <el-button type="primary" id="add" @click="handleAdd">添加</el-button>
                                        </el-form-item>
                                    </el-form>
                                    <div id="addMapContainer" style="width: 95%;height: 450px;margin: 30px auto;"></div>
                                </el-dialog>
                            </div>
                        </div>
                    </el-tab-pane>
                </el-tabs>
            </div>
        </div>

    </body>
    <!-- 引入组件库 -->

    <script src="../js/vue.js"></script>
    <script src="../plugins/elementui/index.js"></script>
    <script type="text/javascript" src="../js/jquery.min.js"></script>
    <script src="../js/axios-0.18.0.js"></script>
    <!--高德地图-->
    <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=9c04b47a5a42fde9f3d8280d08243de8"></script>

    <script>
        let v = new Vue({
            el: '#app',
            data:{
                activeName: 'first',
                pagination: {//分页相关模型数据
					  currentPage: 1,//当前页码
					  pageSize:10,//每页显示的记录数
					  total:0,//总记录数
					  queryString:null//查询条件
				},
				dataList: [],//当前页要展示的分页列表数据
                formData: {
                    address: "",
                    longitude: 0,
                    dimension: 0,
                    remark: ""
                },//表单数据
                dialogFormVisible: false,//增加表单是否可见
                dialogFormVisibleEdit: false,//编辑表单是否可见
                formLabelWidth: "120px",

            },
            //钩子函数，VUE对象初始化完成后自动执行
            created() {
                // vue初始化完成后先加载地图
                this.findAll();
            },
            methods: {
                // 点击tabs标签时
                handleClick(tab, event) {
                    if (tab.name == "first") {
                        this.findAll();
                    }else if (tab.name == "second") {
                        this.findPage();
                    }
                },
                // 地图分布查询
                findAll(){
                    axios.get("/distribution/findAll.do").then((res) =>{
                        if (res.data.flag) {
                            // 地图分布
                            let address = res.data.data;
                            for (let i = 0; i < address.length; i++){
                                let marker = new AMap.Marker({
                                    position: new AMap.LngLat(address[i].longitude,address[i].dimension),
                                    animation: "AMAP_ANIMATION_DROP",
                                });
                                // 清楚以前标记
                                let obj = document.getElementsByClassName("info");
                                for (let i = 0; i < obj.length; i++) {
                                    obj[i].parentNode.parentNode.removeChild(obj[i].parentNode);
                                }
                                // 生成标记位
                                let text = "<div class='info' id='addLabel'>"+address[i].address+"</div>";
                                marker.setLabel({
                                    offset: new AMap.Pixel(5, 5),  //设置文本标注偏移量
                                    content: text, //设置文本标注内容
                                    direction: 'right' //设置文本标注方位
                                });
                                map.add(marker);
                            }
                        }else {
                            this.$message.error(res.data.message);
                        }
                    });
                },
                //分页查询
                findPage() {
                    //发送ajax请求，提交分页相关请求参数（页码、每页显示记录数、查询条件）
                    var param = {
                        currentPage: this.pagination.currentPage,
                        pageSize: this.pagination.pageSize,
                        queryString: this.pagination.queryString
                    };
                    axios.post("/distribution/findPage.do",param).then((res) =>{
                        //解析Controller响应回的数据，为模型数据赋值
                        this.pagination.total = res.data.total;
                        this.dataList = res.data.rows;
                    });
                },
                // 重置表单
                resetForm() {
                    //  用于重置数据
                    this.formData = {};
                },
                // 弹出添加窗口
                handleCreate() {
                    this.dialogFormVisible = true;
                    this.resetForm();
                    var that = this;
                    // 初始化地图
                    this.$nextTick(() => {
                         addmap = new AMap.Map("addMapContainer", {
                            resizeEnable: true,
                            center: [116.397428, 39.90923],//地图中心点
                            zoom: 13,//地图显示的缩放级别
                            keyboardEnable: false
                        });
                        AMap.plugin(['AMap.Autocomplete','AMap.PlaceSearch'],function(){
                            var autoOptions = {
                                city: "全国", //城市，默认全国
                                input: "addInput"//使用联想输入的input的id
                            };
                            autocomplete= new AMap.Autocomplete(autoOptions);
                            var placeSearch = new AMap.PlaceSearch({
                                city:'全国',
                                map:addmap
                            });
                            AMap.event.addListener(autocomplete, "select", function(e){
                                //TODO 针对选中的poi实现自己的功能
                                placeSearch.setCity(e.poi.adcode);
                                placeSearch.search(e.poi.name);
                                if (typeof(e.poi.location.Q) == "undefined" || typeof(e.poi.location.R) == "undefined") {
                                    this.resetForm();
                                }else {
                                    that.formData.address = e.poi.name;
                                    that.formData.longitude = e.poi.location.R;
                                    that.formData.dimension = e.poi.location.Q;
                                }
                            });
                        });
                    });
                },
                //添加
                handleAdd () {
                    // 校验数据
                    if (this.formData.longitude == 0 && this.formData.dimension == 0) {
                        this.$message.error("地址选取错误，请重新选择详细地址...");
                        return;
                    }
                    // 发送请求
                    axios.post("/distribution/add.do",this.formData)
                        .then(resp => {
                            if (resp.data.flag){
                                this.$message({
                                    type:'success',
                                    message:resp.data.message
                                });
                            }else {
                                this.$message.error(resp.data.message);
                            }
                        }).finally(() =>{
                        this.dialogFormVisible = false;
                        this.findPage();
                    })
                },
                //切换页码
                handleCurrentChange(currentPage) {
                    //  设置最新页码
                    this.pagination.currentPage = currentPage;
                    //  重新调用findPage方法进行分页查询
                    this.findPage();
                },
                // 没有权限时提示信息设置
                showMessage(r){
                  if(r == 'Error: Request failed with status code 403'){
                      //权限不足
                      this.$message.error('无访问权限');
                      return;
                  }else {
                      this.$message.error('位置错误');
                  }
                },
                // 删除
                handleDelete(row) {
                    this.$confirm("你确定要删除当前数据吗?","提示",{
                        //  确认框
                        type:'warning'
                    }).then(() => {
                        //  用户点击确定按钮，将检查项id提交到Controller进行处理
                        axios.get("/distribution/delete.do?id="+row.id).then((res) => {
                            if(res.data.flag){
                                //  执行成功，弹出成功提示框
                                this.$message({
                                    type:'success',
                                    message:res.data.message
                                });
                                //  重新进行分页查询
                                this.findPage();
                            }else {
                                //  执行失败
                                this.$message.error(res.data.message)
                            }
                        }).catch((r) => {
                            this.showMessage(r);
                        });
                    }).catch(() => {
                        this.$message({
                            type:'info',
                            message:'操作已取消'
                        });
                    });
                }
            }
        });

        // 地图分布(先初始化Vue)
        var map = new AMap.Map("container",{
            zoom: 11,
            resizeEnable: true,
            center: [114.31,30.52]
        });

    </script>

</html>